package servlet;

import model.DBUtil;
import model.Puser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.sql.*;

@WebServlet("/register")
public class 注册 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String pusername = req.getParameter("pusername");
        String phone = req.getParameter("phone");
        String ppassword = req.getParameter("ppassword");
        String email = req.getParameter("email");
        PrintWriter out = resp.getWriter();
        if(pusername.length()==0||phone.length()==0||ppassword.length()==0||email.length()==0){
            out.print("<script language='javascript'>alert('Please fill in the registration information correctly!!!');window.location.href='/zhuce.html';</script>");
            return;
        }
        Puser puser;
        try {
            puser = save(pusername, phone, ppassword,email);

            HttpSession session = req.getSession();
            session.setAttribute("pcurrentUser", puser);

            //完成登录
            //引导浏览器回到首页
            resp.sendRedirect("/");
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
    }

    private Puser save(String pusername, String phone, String ppassword,String email) throws SQLException {
        ppassword = hash(ppassword);
        try (Connection c = DBUtil.getConnection()) {
            String sql = "INSERT INTO puser(pusername,phone,ppassword,email) VALUES(?,?,?,?)";
            try (PreparedStatement s = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
                s.setString(1, pusername);
                s.setString(2, phone);
                s.setString(3,  ppassword);
                s.setString(4,  email);

                s.executeUpdate();

                try (ResultSet rs = s.getGeneratedKeys()) {
                    if (!rs.next()) {
                        return null;
                    }
                    Puser user = new Puser();
                    user.pusername = pusername;
                    user.phone = phone;
                    user.ppasswprd = ppassword;
                    user.email = email;
                    user.puid = rs.getInt(1);
                    return user;
                }
            }
        }
    }

    private String hash(String ppassword) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
            byte[] bytes = ppassword.getBytes("UTF-8");
            byte[] digest = messageDigest.digest(bytes);
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();

        }catch (Exception exc){
            return ppassword;
        }
    }
}
